package com.admin.common.util;

/**
 * 签名工具类
 */
public class SignUtils {

    private static final String DEFAULT_SECRET = "ys_sign";

    /**
     * 根据参数加密
     * @param objects
     * @return
     */
    public static String sign(Object... objects){
        String sign = "";
        sign+=DEFAULT_SECRET;
        if (null != objects){
            for (Object obj : objects){
                sign+=obj.toString();
            }
        }
        sign+=DEFAULT_SECRET;
        //签名MD5加密
        sign = MD5Utils.string32MD5(sign);
        //转大写
        sign = sign.toUpperCase();
        return sign;
    }


    /**
     * 根据签名和参数验签
     * @param verifySign
     * @param objects
     * @return
     */
    public static boolean verifySign(String verifySign,Object... objects){
        String sign = "";
        sign+=DEFAULT_SECRET;
        if (null != objects){
            for (Object obj : objects){
                sign+=obj.toString();
            }
        }
        sign+=DEFAULT_SECRET;
        //签名MD5加密
        sign = MD5Utils.string32MD5(sign);
        //转大写
        sign = sign.toUpperCase();
        return verifySign.equals(sign);
    }

    public static void main(String[] args) {
        String sign = SignUtils.sign("1", "2", 1);
        System.out.println("sing="+sign);
        boolean b = SignUtils.verifySign(sign, "1", "2", 1);
        System.out.println("验签结果="+b);
    }
}
